"""
工单配置设置
使用pydantic-settings管理工单相关的环境变量配置
"""

from typing import Optional, List
from pydantic_settings import BaseSettings, SettingsConfigDict
from pydantic import Field, model_validator


class TicketSettings(BaseSettings):
    """工单配置类"""
    
    # 工单自动分配配置
    TICKET_AUTO_ASSIGN_ENABLED: bool = Field(
        default=True,
        description="是否启用自动分配工单"
    )
    TICKET_AUTO_RESPONSE_ENABLED: bool = Field(
        default=True,
        description="是否启用自动回复"
    )
    
    # 工单默认值配置
    TICKET_DEFAULT_PRIORITY: str = Field(
        default="medium",
        description="默认工单优先级"
    )
    TICKET_DEFAULT_CATEGORY: str = Field(
        default="general",
        description="默认工单分类"
    )
    
    # 工单超时配置
    TICKET_RESPONSE_TIMEOUT: int = Field(
        default=24,
        description="工单响应超时时间(小时)"
    )
    TICKET_RESOLUTION_TIMEOUT: int = Field(
        default=72,
        description="工单解决超时时间(小时)"
    )
    
    # 工单通知配置
    TICKET_NOTIFICATION_ENABLED: bool = Field(
        default=True,
        description="是否启用工单通知"
    )
    TICKET_ASSIGNMENT_NOTIFICATION: bool = Field(
        default=True,
        description="是否发送工单分配通知"
    )
    TICKET_STATUS_CHANGE_NOTIFICATION: bool = Field(
        default=True,
        description="是否发送工单状态变更通知"
    )
    
    # 工单附件配置
    TICKET_MAX_ATTACHMENTS: int = Field(
        default=5,
        description="每个工单最大附件数量"
    )
    TICKET_MAX_ATTACHMENT_SIZE: int = Field(
        default=10485760,  # 10MB
        description="单个附件最大大小(字节)"
    )
    
    # 工单优先级配置
    TICKET_PRIORITY_LEVELS: List[str] = Field(
        default=["low", "medium", "high", "urgent"],
        description="工单优先级级别"
    )
    
    # 工单分类配置
    TICKET_CATEGORIES: List[str] = Field(
        default=["general", "technical", "billing", "feature_request", "bug"],
        description="工单分类列表"
    )
    
    # 工单状态配置
    TICKET_STATUSES: List[str] = Field(
        default=["open", "in_progress", "resolved", "closed", "reopened"],
        description="工单状态列表"
    )
    
    @model_validator(mode='after')
    def validate_ticket_settings(self) -> 'TicketSettings':
        """验证工单配置"""
        valid_priorities = ["low", "medium", "high", "urgent"]
        if self.TICKET_DEFAULT_PRIORITY not in valid_priorities:
            raise ValueError(f"TICKET_DEFAULT_PRIORITY必须是以下值之一: {valid_priorities}")
        
        valid_categories = ["general", "technical", "billing", "feature_request", "bug"]
        if self.TICKET_DEFAULT_CATEGORY not in valid_categories:
            raise ValueError(f"TICKET_DEFAULT_CATEGORY必须是以下值之一: {valid_categories}")
        
        if self.TICKET_RESPONSE_TIMEOUT <= 0:
            raise ValueError("TICKET_RESPONSE_TIMEOUT必须大于0")
        
        if self.TICKET_RESOLUTION_TIMEOUT <= 0:
            raise ValueError("TICKET_RESOLUTION_TIMEOUT必须大于0")
        
        if self.TICKET_MAX_ATTACHMENTS <= 0:
            raise ValueError("TICKET_MAX_ATTACHMENTS必须大于0")
        
        if self.TICKET_MAX_ATTACHMENT_SIZE <= 0:
            raise ValueError("TICKET_MAX_ATTACHMENT_SIZE必须大于0")
        
        return self

    model_config = SettingsConfigDict(
        env_file=".env",
        env_file_encoding="utf-8",
        case_sensitive=False,
        extra="ignore",
        env_prefix="TICKET_"
    )


# 全局配置实例
ticket_settings = TicketSettings()


def get_ticket_settings() -> TicketSettings:
    """获取工单配置实例"""
    return ticket_settings
